<?php
namespace app\erp_meixingtiyu\logic;

use app\erp_meixingtiyu\model\Booking as BookingModel;
use app\erp_meixingtiyu\model\MemberCardOrder as MemberCardOrderModel;
use app\erp_meixingtiyu\model\CourseOrder as CourseOrderModel;

class Overview
{
    // 获取最近七天的订单数据
    public function getOrderInfoOfSevenDays()
    {
        $dateArr = $this->getLately7days();
        $bookingOrdersOfSevenDays = [];
        $membercardOrdersOfSevenDays = [];
        $courseOrdersOfSevenDays = [];

        $salesOfSevenDays = [];
        $bookingSalesOfSevenDays = [];
        $membercardSalesOfSevenDays = [];
        $courseSalesOfSevenDays = [];

        $arenaInfo = [];
        $cardInfo = [];
        $courseInfo = [];

        $len = count($dateArr);
        foreach ($dateArr as $key => $date) {
            // 当天订单数量
            $arenaOrders = $this->getArenaOrderOfToday($date);
            $cardOrders = $this->getCardOrderOfToday($date);
            $courseOrders = $this->getCourseOrderOfToday($date);
            // 最近七天的订单数量
            $bookingOrdersOfSevenDays[] = $arenaOrders['orderNums'];
            $membercardOrdersOfSevenDays[] = $cardOrders['orderNums'];
            $courseOrdersOfSevenDays[] = $courseOrders['orderNums'];
            // 每天总销售额
            $salesOfSevenDays[] =
                $arenaOrders['totalSalesAmount'] +
                $cardOrders['totalSalesAmount'] +
                $courseOrders['totalSalesAmount'];
            // 最近七天单日销售额
            $bookingSalesOfSevenDays[] = $arenaOrders['totalSalesAmount'];
            $membercardSalesOfSevenDays[] = $cardOrders['totalSalesAmount'];
            $courseSalesOfSevenDays[] = $courseOrders['totalSalesAmount'];

            if ($key === $len - 1) {
                $arenaInfo = $arenaOrders;
                $cardInfo = $cardOrders;
                $courseInfo = $courseOrders;
            }
        }

        return [
            'arenaInfo' => $arenaInfo,
            'cardInfo' => $cardInfo,
            'courseInfo' => $courseInfo,
            'tradeTrend' => [
                'date' => $dateArr,
                'bookingOrdersOfSevenDays' => $bookingOrdersOfSevenDays,
                'membercardOrdersOfSevenDays' => $membercardOrdersOfSevenDays,
                'courseOrdersOfSevenDays' => $courseOrdersOfSevenDays,
                'salesOfSevenDays' => $salesOfSevenDays,
                'bookingSalesOfSevenDays' => $bookingSalesOfSevenDays,
                'membercardSalesOfSevenDays' => $membercardSalesOfSevenDays,
                'courseSalesOfSevenDays' => $courseSalesOfSevenDays,
            ],
        ];
    }

    /**
     * 最近七天日期
     */
    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }

    // 获取今日的订场数据
    public function getArenaOrderOfToday($date)
    {
        $list = (new BookingModel())->getOrdersByDate($date);
        // 销售总金额
        $totalSalesAmount = 0;
        // 订单总量
        $orderNums = count($list);
        foreach ($list as $order) {
            $totalSalesAmount = ($totalSalesAmount * 100 + $order->total_price * 100) / 100;
        }

        return compact('totalSalesAmount', 'orderNums');
    }

    // 获取今日的会员卡订单数据
    public function getCardOrderOfToday($date)
    {
        $list = (new MemberCardOrderModel())->getOrdersByDate($date);
        // 销售总金额
        $totalSalesAmount = 0;
        // 订单总量
        $orderNums = count($list);
        foreach ($list as $order) {
            $totalSalesAmount = ($totalSalesAmount * 100 + $order->price * 100) / 100;
        }

        return compact('totalSalesAmount', 'orderNums');
    }

    // 获取今日的课程订单数据
    public function getCourseOrderOfToday($date)
    {
        $list = (new CourseOrderModel())->getOrdersByDate($date);
        // 销售总金额
        $totalSalesAmount = 0;
        // 订单总量
        $orderNums = count($list);
        foreach ($list as $order) {
            $totalSalesAmount = ($totalSalesAmount * 100 + $order->price * 100) / 100;
        }

        return compact('totalSalesAmount', 'orderNums');
    }
}
